package com.my.learn.nio.netty.longConnection;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.protostuff.LinkedBuffer;
import io.protostuff.ProtostuffIOUtil;
import io.protostuff.Schema;
import io.protostuff.runtime.RuntimeSchema;

/**
 * @author fu
 * @version 1.0
 * @description TODO
 * @date 2021-11-05 15:46
 */
public class ProtostuffEncoder extends MessageToByteEncoder<SendModel> {

    @Override
    protected void encode(ChannelHandlerContext ctx, SendModel msg, ByteBuf out) throws Exception {
        LinkedBuffer buffer = LinkedBuffer.allocate(1024);
        Schema<SendModel> sendModelSchema = RuntimeSchema.getSchema(SendModel.class);
        byte[] bytes = ProtostuffIOUtil.toByteArray(msg, sendModelSchema, buffer);
        out.writeBytes(bytes);
    }

}
